package com.example.javanewwork.controller;

import com.example.javanewwork.entity.Result;
import com.example.javanewwork.entity.User;
import com.example.javanewwork.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        System.out.println(user);
        String username = user.getUsername();
        String password = user.getPassword();
        String Email = user.getEmail();
        System.out.println(Email);
        String phone = user.getPhone();
        Integer UserType = user.getUserType();
        User u = userService.findByUserName(username);
        if (u==null){
            userService.register(username,password,Email,phone,UserType);
            return Result.success();
        }else{
            return Result.error("用户名已被占用");
        }

    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        String Email = user.getEmail();
        String phone = user.getPhone();
        Integer UserType = user.getUserType();
        User u = userService.findByUserName(username);
        //根据用户名查询用户
        User loginUser = userService.findByUserName(username);
        //判断该用户是否存在
        if(loginUser == null){
            return Result.success("no");
        }

        //判断密码是否正确
        if(password.equals(loginUser.getPassword())){
            if(username == "admin"){
                return Result.success("admin");
            }
            return Result.success(username);
        }
        return Result.success("no");
    }
}